# 导包
import jsonschema

# 准备效验数据
data = {
    "name": "盛昌柱",
    "sex": "男",
    "like": "女",
    "age": 21,
    "height": 170.01,
    "shuai": True,
    "likeNumber": [7, 8, 12],
    "old": None,
    "play": {
        "hero": "艾琳",
        "character": "Blood sucking count"
    }
}

# 使用jsonschema进行规范
schema = {
    "type": "object",
    "properties": {
        "name": {"pattern": "^盛"},
        "sex": {"type": "string"},
        "like": {"const": "女"},
        "age": {"const": 21},
        "height": {"type": "number"},
        "shuai": {"type": "boolean"},
        "likeNumber": {"type": "array"},
        "play": {
            "type": "object",
            'properties': {
                "hero": {"type": "string"},
                "character": {"pattern": "count$"}
            },
            "required": ["hero", "character"]
        }
    },
    "required": ["name", "sex", "like", "age", "height", "shuai", "likeNumber", "play"]
}
#使用validate进行效验
res = jsonschema.validate(instance=data, schema=schema)
print("res=", res)
